home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * AppleEvent.c
- *
- * Functions related to processing AppleEvents and dispatching to the appropriate handlers
- *
- ****************************************************************************/
-
- #include <string.h>
- #include "StringUtils.h"
-
- #include "AppleEvent.h"
-
- #include "AERRequiredSuite.h"
- #include "AERCoreSuite.h"
- #include "AERMiscSuite.h"
- #include "OSLHelpers.h"
-
-
- /*****************************************************************************
- *
- * InstallAEHandlers
- *
- * Call this at program initialization time
- *
- *****************************************************************************/
-
- Boolean
- InstallAEHandlers()
- {
- Boolean success = HasAppleEvents();
-
- // The wildcard/wildcard event handler gets every event that does not have
- // a specific event handler installed.
-
- if (success)
- success = (AEInstallEventHandler(typeWildCard,
- typeWildCard,
- NewAEEventHandlerProc(HandleEveryOtherAppleEvent),
- 0,
- false) == noErr);
-
- if (success)
- success = InstallRequiredSuiteHandlers();
-
- if (success)
- success = InstallCoreSuiteHandlers();
-
- if (success)
- success = InstallMiscSuiteHandlers();
-
- if (success)
- success = InstallObjectCallbackFunctions();
-
- if (success)
- success = InstallCoercionHandlers();
-
- return success;
- }
-
- /*****************************************************************************
- *
- * HasAppleEvents()
- *
- * Does the system software support AppleEvents?
- *
- *****************************************************************************/
-
- Boolean
- HasAppleEvents(void)
- {
- Boolean hasIt = false;
- long result = 0L;
-
- OSErr error = Gestalt(gestaltAppleEventsAttr, &result);
-
- hasIt = (result & (1 << gestaltAppleEventsPresent));
-
- return hasIt;
- }
-
- //----------------------------------------------------------------------------------
- // This is a wildcard/wildcard handler that receives Apple events which
- // do not have a specific handler installed.
- // This can be useful for debugging. You'll find out every event your application
- // receives that you don't explicity handle, if you set a breakpoint in this function.
- //----------------------------------------------------------------------------------
-
- pascal OSErr HandleEveryOtherAppleEvent(AppleEvent *appleEvent, AppleEvent *reply, long refCon)
- {
- #pragma unused (reply, refCon)
- OSErr error = noErr;
- AEEventClass eventClass;
- AEEventID eventID;
- OSType typeCode;
- Size actualSize = 0L;
-
- // Get the event class
-
- error = AEGetAttributePtr(appleEvent,
- keyEventClassAttr,
- typeType,
- &typeCode,
- (Ptr)&eventClass,
- sizeof(eventClass),
- &actualSize);
-
- // Get the event ID
-
- error = AEGetAttributePtr(appleEvent,
- keyEventIDAttr,
- typeType,
- &typeCode,
- (Ptr)&eventID,
- sizeof(eventID),
- &actualSize);
-
- // We ignore recording events for now
-
- switch (eventID)
- {
- case 'rec0': // Recording turned off
- case 'rec1': // Recording turned on
- return noErr;
- }
-
- return errAEEventNotHandled; // to give system handlers a chance at the event
- }
-
-
- //----------------------------------------------------------------------------------
- // Check to see if there exists any additional required parameters in the Apple Event.
- // If so, return an error to the calling routine, because we didn't extract them all.
- //----------------------------------------------------------------------------------
-
- OSErr
- CheckForUnusedParameters(const AppleEvent* appleEvent)
- {
- OSErr error = noErr;
-
- DescType actualType = typeNull;
- Size actualSize = 0L;
-
- error = AEGetAttributePtr(appleEvent,
- keyMissedKeywordAttr,
- typeWildCard,
- &actualType,
- nil,
- 0,
- &actualSize);
-
- if (error == errAEDescNotFound)
- error = noErr;
- else
- error = errAEParamMissed;
-
- return error;
- }
-
- /*****************************************************************************
- *
- * PutReplyErrorNumber
- *
- * If a reply is expected, the error number is returned in the reply parameter.
- *
- *****************************************************************************/
-
- OSErr
- PutReplyErrorNumber(AppleEvent* reply, long errorNumber)
- {
- OSErr error = noErr;
-
- if (reply->dataHandle != nil && errorNumber != noErr)
- error = AEPutParamPtr(reply,
- keyErrorNumber,
- typeLongInteger,
- (Ptr)&errorNumber,
- sizeof(long));
-
- return error;
- }
-
- /*****************************************************************************
- *
- * PutReplyErrorMessage
- *
- * If a reply is expected, the error message is inserted into the reply parameter.
- *
- *****************************************************************************/
-
- OSErr
- PutReplyErrorMessage(AppleEvent* reply, char *message)
- {
- OSErr error = noErr;
-
- if (reply->dataHandle != nil && message != NULL)
- error = AEPutParamPtr(reply,
- keyErrorString,
- typeChar,
- (Ptr)message,
- strlen(message));
-
- return error;
- }
-
- // ------------------------------------------------------------------------
- // This is used to extract the type of object that an appleevent is supposed
- // to work with. In the Core events, this includes:
- // count <reference> each <typeOfObject>
- // make new <typeOfObject>
- // save <reference> in <alias> as <typeOfObject>
- // ------------------------------------------------------------------------
-
- OSErr
- GetObjectClassFromAppleEvent(const AppleEvent *appleEvent, DescType *objectClass)
- {
- OSErr error = noErr;
- OSType typeCode; // should be typeType
- long actualSize;
-
- // Get the class of object that we will count
-
- error = AEGetParamPtr(appleEvent,
- keyAEObjectClass,
- typeType,
- &typeCode,
- (Ptr)objectClass,
- sizeof(DescType),
- &actualSize);
-
- if (typeCode != typeType)
- error = errAECoercionFail;
-
- return error;
- }
-
- // ------------------------------------------------------------------------
-
-